<?php

class AdminAction extends Action {

    public function index() {
        $Admin = D("Admin");
        $count = $Admin->count();
        import("ORG.Util.Page");
        $listRows = 15;
        $p = new page($count, $listRows);
        $list = $Admin->findAll(null, '*', 'addtime DESC', $p->firstRow . ',' . $p->listRows);
        $page = $p->show();
        if ($list !== false) {
            $this->assign('page', $page);
            $this->assign('list', $list);
        }
        $this->display();
    }

    public function add() {
        if (!$this->isPost()) {
            $this->display();
        } else {
            $username = isset($_POST['username']) ? trim($_POST['username']) : '';
            if (strlen($username) < 6 || strlen($username) > 16) {
                $this->error("用户名为6到16个字符组成");
                return;
            }
            $password = isset($_POST['password']) ? trim($_POST['password']) : '';
            if (strlen($password) < 6 || strlen($password) > 16) {
                $this->error("用户密码为6到16个字符组成");
                return;
            }
            $repwd = isset($_POST['repwd']) ? trim($_POST['repwd']) : '';
            if ($password != $repwd) {
                $msg = "两次输入的密码不一致";
                $this->error($msg);
                return;
            }
            $relname = isset($_POST['relname']) ? trim($_POST['relname']) : '';
            $email = isset($_POST['password']) ? trim($_POST['email']) : '';
            $qq = isset($_POST['qq']) ? trim($_POST['qq']) : '';
            $data = array(
                'username' => $username,
                'password' => md5($password),
                'relname' => $relname,
                'email' => $email,
                'qq' => $qq,
            );

            $Admin = D("Admin");
            if (!$Admin->create($data)) {
                $this->error($Admin->getError());
                return;
            }
            if (!$Admin->add()) {
                $this->error(L('_INSERT_FAIL_'));
                return;
            }
            $this->assign('jumpUrl', __URL__);
            $this->success(L('_INSERT_SUCCESS_'));
            return;
        }
    }

    public function edit() {
        if (!$this->isPost()) {
            $id = isset($_GET['id']) ? (int) ($_GET['id']) : 0;
            if (!$id) {
                $this->error(L('_SELECT_NOT_EXIST_'));
                return;
            }
            $Admin = D("Admin");
            $list = $Admin->find($id);
            if (!$list) {
                $this->error(L('_SELECT_NOT_EXIST_'));
                return;
            }
            $this->assign('vo', $list);
            $this->display();
        } else {
            $id = isset($_POST['id']) ? (int) ($_POST['id']) : 0;
            $S = D('Admin')->find($id);
            if (!$id || !$S) {
                $this->error(L('_SELECT_NOT_EXIST_'));
                return;
            }
            $password = isset($_POST['password']) ? trim($_POST['password']) : '';
            if ($password != '' && (strlen($password) < 6 || strlen($password) > 16)) {
                $this->error('用户密码为6到16个字符组成');
                return;
            }
            $repwd = isset($_POST['repwd']) ? trim($_POST['repwd']) : '';
            if ($password != '' && $password != $repwd) {
                $this->error('两次输入的密码不一致');
                return;
            }

            $relname = isset($_POST['relname']) ? trim($_POST['relname']) : '';
            $email = isset($_POST['email']) ? trim($_POST['email']) : '';
            $qq = isset($_POST['qq']) ? trim($_POST['qq']) : '';

            $data = array(
                'id' => $id,
                'relname' => $relname,
                'email' => $email,
                'qq' => $qq,
            );
            if ($password != ''
                )$data['password'] = $password;
            $Admin = D("Admin");
            if (!$Admin->create($data)) {
                $this->error($Admin->getError());
                return;
            }
            if (!$Admin->save()) {
                $this->error(L('_UPDATE_FAIL_'));
                return;
            }
            $this->assign('jumpUrl', __URL__);
            $this->success(L('_UPDATE_SUCCESS_'));
        }
    }

    public function del() {
        $id = isset($_GET['id']) ? (int) ($_GET['id']) : 0;
        if (!$id) {
            $this->error(L('_SELECT_NOT_EXIST_'));
            return;
        }
        $Admin = D("Admin");
        $list = $Admin->find($id);
        if (!$list) {
            $this->error(L('_SELECT_NOT_EXIST_'));
            return;
        }
        if (!$Admin->deleteById($id)) {
            $this->error(L('_DELETE_FAIL_'));
            return;
        }
        $this->redirect();
    }

}

?>